// 生成随机随机32 位  字符串
exports.getRnd32 = () => {
    let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    let result = ''
    for (let i = 0; i < 32; i++) {
        let rnd = Math.floor(Math.random() * str.length)
        result += str[rnd]
    }
    return result
}


// 生成时间戳
exports.createTimeStamp = () => {
    return parseInt(new Date().getTime() / 1000) + '';
}


// 生成订单编号
exports.createOrderNumber = () => {
    let str = ''
    for (let i = 0; i < 10; i++) {
        let num = Math.floor(Math.random() * 10)
        str += num
    }
    let time = new Date()
    let year = time.getFullYear().toString()
    let month = time.getMonth().toString() + 1
    let day = time.getDate().toString()
    let hours = time.getHours().toString()
    let minutes = time.getMinutes().toString()
    let seconds = time.getSeconds().toString()
    let mill = time.getMilliseconds().toString()
    str += year += month += day += hours += minutes += seconds += mill
    return str;
}


// 按照ascll码排序
function raw(args) {
    var keys = Object.keys(args);
    keys = keys.sort()
    var newArgs = {};
    keys.forEach(function (key) {
        newArgs[key] = args[key];
    });
    var string = '';
    for (var k in newArgs) {
        string += '&' + k + '=' + newArgs[k];
    }
    string = string.substr(1);
    return string;
}


/**
 * 企业付款到零钱签名
 */
exports.transfersSign = (map) => {
    let ret = {
        mch_appid: map.mch_appid,
        mchid: map.mchid,
        nonce_str: map.nonce_str,
        partner_trade_no: map.partner_trade_no,
        openid: map.openid,
        check_name: map.check_name,
        amount: map.amount,
        desc: map.desc,
        spbill_create_ip: map.spbill_create_ip,
    }
    //console.log("签名", ret)
    var string = raw(ret);
    var key = map.mchkey;
    string = string + '&key=' + key;
    var crypto = require('crypto');
    return crypto.createHash('md5').update(string, 'utf8').digest('hex').toUpperCase();
}